#include<stdio.h>

int DR_pow256(int a)
{
	int sum = 1;
	int i = 0;
	while (i < a)
	{
		sum = sum * 256;
		i++;
	}
	return sum;
}

int DR_readInt(FILE *fp, int bit)
{
	char buf[16];
	int res = 0;
	int i = bit - 1;
	int tmp;
	int pows;
	while (i >= 0)
	{
		fread(buf, 1, 1, fp);
		tmp = buf[0];
		if (tmp < 0)
		{
			tmp = tmp + 256;
		}
		pows = DR_pow256(i);
		res = res + pows*tmp;
		i = i - 1;
	}
	return res;
}

int DR_scanInt(FILE *fp, int bit)
{
	if(fp==0) return 0;
	int res = 0;
	int i = bit - 1;
	int tmp;
	int pows;
	while (i >= 0)
	{
		fscanf(fp, "%c", &tmp);
		tmp=tmp%256;
		pows = DR_pow256(i);
		res = res + pows*tmp;
		i = i - 1;
	}
	return res;
}

int filescan(char *addr)
{
	int tmp;
	int res=0;
	FILE* fp=fopen(addr, "rb");
	if(fp==NULL) return 1;
	for(int i=0;i<4;++i)
    {
        for(int j=3;j>=0;--j)
		{
			fscanf(fp,"%c",&tmp);
			res=res+tmp*DR_pow256(j);
		}
		printf("%d ",res);
		res=0;
    }
	printf("\n");
    for(int i=0;i<100;++i)
    {
        fscanf(fp, "%c", &tmp);
        printf("%d ",tmp);
    }
    fclose(fp);
    return 0;
}

int main()
{
    FILE* fp=fopen(".\\dataset\\train-images.idx3-ubyte", "rb");
    if(fp==NULL) return 0;
    int tmp;
    for(int i=0;i<4;++i)
    {
        printf("%d\n",DR_scanInt(fp,4));
    }
    for(int i=0;i<200;++i)
    {
        // printf("%d\n",DR_scanInt(fp,1));
        fscanf(fp, "%c", &tmp);
        printf("%d ",tmp);
    }
    fclose(fp);
    return 0;
}